package com.learn.proxy.impl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy<T> {

    private T target;

    public DynamicProxy(T target) {
        this.target = target;
    }

    public T getProxy() {
        ClassLoader classLoader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object targetMethodReturnValue = null;
                String name = method.getName();
                try {
                    targetMethodReturnValue = method.invoke(args);
                }catch (Exception e) {
                    e.printStackTrace();
                }
                return targetMethodReturnValue;
            }
        };
        T proxy = (T) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
        return proxy;
    }
}
